define(['zepto', 'butterfly/view','../../bsl',
    'text!./oauth.html'
], function($, View, bsl, oauth) {
    var me = null;

    var view = View.extend({
        events: {

            // ！！！！ 由于 引入 touch.js 后，tap事件会变成双击，所以 本页面内不能使用tap，用click代替。
            "tap #openDoc" : "openDoc",
            "tap .logoutAll" : "logoutAll",
            "tap #back" : "back",

      
        },
        render: function() {
            me = this;
        },

        onShow: function() {

            // 绑定自定义事件 滑动切换日历等           


        },
                 auths:{},
        bslReady:function (){
            // 获取登录认证通道
            bsl.oauth.getServices(function(services){
                var content=document.getElementById('dcontent');
                var info=document.getElementById("info");
                var txt="登录认证通道信息：";
                for(var i in services){
                    var service=services[i];
                    console.log(service.id+": "+service.authResult+", "+service.userInfo);
                    auths[service.id]=service;
                    txt += "id:"+service.id+", ";
                    txt += "description:"+service.description+", ";
                    var de=document.createElement('div');
                    de.setAttribute('class','button');
                    de.setAttribute('onclick','login(this.id)');
                    de.id=service.id;
                    de.innerText=service.description+"登录";
                    oauth.appendChild(de);
                }
                info.innerText=txt;
            },function(e){
                console.info("获取登录认证失败："+e.message);
            });
        },
        // document.addEventListener('bslready',bslReady,false);
        // 登录认证
        login:function (id){
            outSet("----- 登录认证 -----");
            var auth=auths[id];
            if(auth){
                var w=bsl.nativeUI.showWaiting();
                document.addEventListener("pause",function(){
                    setTimeout(function(){
                        w&&w.close();w=null;
                    },2000);
                }, false );
                auth.login(function(){
                    w&&w.close();w=null;
                    console.info("登录认证成功：");
                    console.info(JSON.stringify(auth.authResult));
                    userinfo(auth);
                },function(e){
                    w&&w.close();w=null;
                    console.info("登录认证失败：");
                    console.info("["+e.code+"]："+e.message);
                    bsl.nativeUI.alert("详情错误信息请参考授权登录(OAuth)规范文档：http://www.html5bsl.org/#specification#/specification/OAuth.html",null,"登录失败["+e.code+"]："+e.message);
                });
            }else{
                console.info("无效的登录认证通道！");
                bsl.nativeUI.alert("无效的登录认证通道！",null,"登录");
            }
        },
        // 获取用户信息
        userinfo:function (a){
            console.info("----- 获取用户信息 -----");
            a.getUserInfo(function(){
                console.info("获取用户信息成功：");
                console.info(JSON.stringify(a.userInfo));
                var nickname=a.userInfo.nickname||a.userInfo.name;
                bsl.nativeUI.alert("欢迎“"+nickname+"”登录！");
            },function(e){
                console.info("获取用户信息失败：");
                console.info("["+e.code+"]："+e.message);
                bsl.nativeUI.alert("获取用户信息失败！",null,"登录");
            });
        },
        // 注销登录
        logoutAll:function (){
            outSet("----- 注销登录认证 -----");
            for(var i in auths){
                logout(auths[i]);
            }
        },
        logout:function (auth){
            auth.logout(function(){
                console.info("注销\""+auth.description+"\"成功");
            },function(e){
                console.info("注销\""+auth.description+"\"失败："+e.message);
            });
        },
          back : function(e){
            window.history.go(-1);
        },
        openDoc:function(){
             window.butterfly.navigate('com.no5.cordovademo/doc/oauth', {
                trigger: true
            });


        },
      
             
            });
    return view;
});
